package dao;
import tool.RowMapper;
import tool.BaseDao;
import java.util.List;
import java.sql.SQLException;
import java.sql.ResultSet;
import entity.GoodstbEntity;
import dao.mapper.GoodstbDAOI;

/**
*@author JiangFan
*/
public class GoodstbDAO extends BaseDao implements GoodstbDAOI {

/*
 * 用于查询单个商品,根据商品名称查询
 */
	public List<GoodstbEntity> selectGoods_goodname(String Goodname) {
		String sql="SELECT * FROM goodstb WHERE goods_name=?";
		Object[] param = {Goodname};
		List<GoodstbEntity> list = null;
			try {
				list = executeQuery(sql,param,new GoodstbRowMapper());
			} catch (ClassNotFoundException | SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		
		return list;
	}
	/**
	 * 
	 * 	
	 */
	public List<GoodstbEntity> selectlike(String Goodname) {
		String sql="SELECT * FROM goodstb WHERE goods_name like'%?%'";
		Object[] param = {Goodname};
		List<GoodstbEntity> list = null;
		
			try {
				list = executeQuery(sql,param,new GoodstbRowMapper());
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		
		return list;
	}
	/**
	 * 
	 *  * 根据商品类别查询商品，用于级联菜单
	 */
	public List<GoodstbEntity> selectGoods_goods_type(String Goodtype) {
		String sql="SELECT * FROM goodstb WHERE goods_type=? ";
		Object[] param = {Goodtype};
		List<GoodstbEntity> list = null;
		
			try {
				list = executeQuery(sql,param,new GoodstbRowMapper());
			} catch (ClassNotFoundException | SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		
		return list;
	}

	/**
	 * @param goods_id
	 *         根据用户编号查询用户
	 * @return
	 */
	@Override
	public GoodstbEntity selectGoodsOne (String goods_id) {
		return null;
	}

	/**添加商品
	 * @param goods
	 *         传入实体类
	 * @return
	 * @throws SQLException
	 * @throws ClassNotFoundException
	 */
	@Override
	public int insertGood (GoodstbEntity goods) throws SQLException, ClassNotFoundException {
		String sql="insert into goodstb values(?,?,?,?,?,?,?)";
		Object[] param={
				goods.getGoods_id (),
				goods.getGoods_name (),
				goods.getGoods_picture (),
				goods.getGoods_type (),
				goods.getGoods_model (),
				goods.getGoods_note (),
				goods.getGoods_state ()
		};
		return update (sql,param);
	}
}


class GoodstbRowMapper implements RowMapper<GoodstbEntity>{
	@Override
	public GoodstbEntity rowmap(ResultSet set) throws  SQLException{
		return new GoodstbEntity(
			set.getString("goods_id"),
			set.getString("goods_name"),
			set.getString("goods_picture"),
			set.getString("goods_type"),
			set.getString("goods_model"),
			set.getString("goods_note"),
			set.getString("goods_state")
		);
	}
}
